from lxml import etree
import requests
import os
if __name__ == '__main__':
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36'
    }
    url = 'https://jn.58.com/ershoufang/'
    res = requests.get(url=url,headers=headers).text
    tree = etree.HTML(res)
    content_list = tree.xpath('//div[@class="ntable"]')
    f = open('58二手房.txt','w',encoding='utf-8')
    for item in content_list:
        title = item.xpath('.//div[@class="property-content-title"]/h3/text()')[0]
        f.write(title+'\n')
    f.close()
    print('爬取完毕！')